package vv.lines.view.swing;

import vv.lines.view.TextDialogView;
import vv.lines.view.ViewAdapter;
import vv.lines.view.WindowView;
import vv.lines.view.swing.utils.SwingResourceHolder;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import static javax.swing.event.HyperlinkEvent.EventType.ACTIVATED;
import static vv.lines.utils.TextResources.OK;
import static vv.lines.view.swing.utils.SwingLinesConstants.TEXT_HTML;

/**
 * @author vpotapenko
 */
public class SwingTextDialogView extends ViewAdapter implements TextDialogView {

    private static final Logger logger = Logger.getLogger(SwingTextDialogView.class.getName());

    private JDialog dialog;
    private JTextPane textPane;

    public SwingTextDialogView(SwingResourceHolder resourceHolder) {
        initDialog(resourceHolder);
    }

    private void initDialog(SwingResourceHolder resourceHolder) {
        dialog = new JDialog((Frame) null, true);
        dialog.setIconImage(resourceHolder.getGameImage());

        Container contentPane = dialog.getContentPane();
        contentPane.setLayout(new BorderLayout());

        final JLabel label = new JLabel(resourceHolder.getGameIcon());
        contentPane.add(label, BorderLayout.WEST);

        textPane = new JTextPane();
        textPane.setContentType(TEXT_HTML);
        textPane.setEditable(false);
        textPane.addHyperlinkListener(new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent event) {
                if (event.getEventType() == ACTIVATED) {
                    activateURL(event);
                }
            }
        });

        contentPane.add(new JScrollPane(textPane), BorderLayout.CENTER);

        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());
        box.add(createOkButton());
        box.add(Box.createHorizontalGlue());

        contentPane.add(box, BorderLayout.SOUTH);
    }

    protected JButton createOkButton() {
        JButton button = new JButton(OK);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        });
        setDefaultButton(button);
        return button;
    }

    protected void setDefaultButton(JButton button) {
        dialog.getRootPane().setDefaultButton(button);
    }

    private void activateURL(HyperlinkEvent event) {
        try {
            final URI uri = event.getURL().toURI();
            Desktop.getDesktop().browse(uri);
        } catch (Exception e) {
             logger.log(Level.SEVERE, e.getMessage(), e);
        }
    }

    public void setText(String text) {
        textPane.setText(text);
    }

    public void show(WindowView owner) {
        dialog.pack();
        dialog.setLocationRelativeTo((Component) owner);
        dialog.setVisible(true);
    }

    @Override
    public void setTitle(String title) {
        dialog.setTitle(title);
    }

    @Override
    public void setTextSize(int width, int height) {
        textPane.setPreferredSize(new Dimension(width, height));
    }
}
